package org.example.springcloud.event;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

@Component
public class DataListener implements ApplicationListener<DataEvent> {

    @Autowired
    private DruidDataSource dataSource;

    @Override
    public void onApplicationEvent(DataEvent event) {
        dataSource.setPassword(event.getPassword());
        try {
            dataSource.restart();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(dataSource.toString());
    }
}
